package com.hibase.core.excel.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.hibase.common.exception.HibaseAssert;
import com.hibase.core.excel.entity.MdsExcelBizDataDetail;
import com.hibase.core.excel.entity.MdsExcelImportConfigDetail;
import com.hibase.core.excel.mapper.MdsExcelImportConfigDetailMapper;
import com.hibase.core.excel.service.MdsExcelBizDataDetailService;
import com.hibase.core.excel.service.MdsExcelImportConfigDetailService;
import com.hibase.core.excel.util.Validate;
import com.hibase.core.excel.vo.config.detail.MdsExcelImportConfigDetailSearchVO;
import com.hibase.core.mybatis.service.BaseServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Excel 模板配置详情表 Service 实现类
 *
 * @author <a href="mailto:xiongliyi@hichain.com">Ian Xiong</a>
 * @since 1.4.35-V2
 */
@AllArgsConstructor
@Service
public class MdsExcelImportConfigDetailServiceImpl extends BaseServiceImpl<MdsExcelImportConfigDetailMapper, MdsExcelImportConfigDetail> implements MdsExcelImportConfigDetailService {

    private final MdsExcelBizDataDetailService mdsExcelBizDataDetailService;


    @Override
    public PageInfo queryPage(MdsExcelImportConfigDetailSearchVO searchVO) {
        QueryWrapper<MdsExcelImportConfigDetail> queryWrapper  = new QueryWrapper<>();
        queryWrapper.lambda().eq(MdsExcelImportConfigDetail::getBeDeleted, Boolean.FALSE);

        if (StrUtil.isNotEmpty(searchVO.getKeyWord())) {
            queryWrapper.lambda().like(MdsExcelImportConfigDetail::getColumnName, searchVO.getKeyWord());
        }

        if (StrUtil.isNotEmpty(searchVO.getColumnGroupName())) {
            queryWrapper.lambda().like(MdsExcelImportConfigDetail::getColumnGroupName, searchVO.getColumnGroupName());
        }

        if (StrUtil.isNotEmpty(searchVO.getColumnName())) {
            queryWrapper.lambda().like(MdsExcelImportConfigDetail::getColumnName, searchVO.getColumnName());
        }

        if (StrUtil.isNotEmpty(searchVO.getBizImportConfigId())) {
            queryWrapper.lambda().eq(MdsExcelImportConfigDetail::getBizImportConfigId, searchVO.getBizImportConfigId());
        }


        queryWrapper.lambda().orderByDesc(MdsExcelImportConfigDetail::getColumnNum);


        return super.page(searchVO, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveImportConfigDetail(MdsExcelImportConfigDetail entity) {
        hasBizDataId(entity.getId(), entity.getBizImportConfigId(), entity.getBizDataDetailId(), Boolean.FALSE);
        return baseMapper.insert(entity) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateImportConfigDetail(MdsExcelImportConfigDetail entity) {
        hasBizDataId(entity.getId(), entity.getBizImportConfigId(), entity.getBizDataDetailId(), Boolean.TRUE);
        return baseMapper.updateById(entity);
    }


    @Override
    public List<MdsExcelImportConfigDetail> findByBizImportConfigId(String mdsExcelImportConfigId) {
        return baseMapper.selectList(Wrappers.<MdsExcelImportConfigDetail>lambdaQuery()
                .eq(MdsExcelImportConfigDetail::getBizImportConfigId, mdsExcelImportConfigId)
                .eq(MdsExcelImportConfigDetail::getBeActive, Boolean.TRUE)
                .eq(MdsExcelImportConfigDetail::getBeDeleted, Boolean.FALSE)
                .orderByAsc(MdsExcelImportConfigDetail::getColumnNum));
    }

    @Override
    public Map<String, MdsExcelImportConfigDetail> findMapByBizImportConfigIdToMap(String mdsExcelImportConfigId) {
        List<MdsExcelImportConfigDetail> list = findByBizImportConfigId(mdsExcelImportConfigId);
        HibaseAssert.notNull(list, "Excel_Import_Config_Detail items cannot be null");
        return list.stream().collect(Collectors.toMap(item -> item.getBizDataDetailId(), item -> item));
    }


    private void hasBizDataId(String id, String bizImportConfigId, String bizDataDetailId, Boolean isUpdate) {
        QueryWrapper<MdsExcelImportConfigDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda() .eq(MdsExcelImportConfigDetail::getBizImportConfigId, bizImportConfigId)
                .eq(MdsExcelImportConfigDetail::getBizDataDetailId, bizDataDetailId);
        if (isUpdate) {
            queryWrapper.lambda().ne(MdsExcelImportConfigDetail::getId, id);
        }

        Integer count = baseMapper.selectCount(queryWrapper);
        if (count != 0) {
            MdsExcelBizDataDetail mdsExcelBizDataDetail = mdsExcelBizDataDetailService.getById(bizDataDetailId);
            Validate.isTrue(count == 0, mdsExcelBizDataDetail.getFieldName() + "already exists");
        }
    }


}
